from django.shortcuts import render
from rest_framework.generics import ListAPIView
# Create your views here.
from rest_framework.pagination import PageNumberPagination
from rest_framework.filters import OrderingFilter
from .models import SKU
from .serializers import SKUModelSerializer, SKUIndexHaystackSerializer
from drf_haystack.viewsets import HaystackViewSet



class SKUListPageNumberPagination(PageNumberPagination):
    """
    自定义分页器
    """
    page_size = 5
    page_query_param = 'page'
    max_page_size = 20

#要么就继承，使用他的方法或者属性，设置该类为全局变量
class SKUListView(ListAPIView):
# 这里不需要要再继承了
    """

    """
    pagination_class = SKUListPageNumberPagination
    # 因为底下已经用这个参数指定了，可能是这里的原因检查完其他的如果没问题，那报错就来自于这
    #关键字 实现排序功能
    filter_backends = (OrderingFilter,)
    ordering_fields = ('price','-sales','-create_time')
    serializer_class = SKUModelSerializer

    #修改查询数据
    def get_queryset(self):
        category_id =  self.kwargs['category_id']
        # 你这小错误犯的确实不少，参数名都能搬错，category_id,这啥
        return SKU.objects.filter(category_id=category_id,is_launched=True)
        # 属实没眼看，一个错误导致地下的参数名也是错的，错误的参数名配错误的参数值
    # 导致我看你代码必须从头检查到尾，毕竟永远不知道是哪里敲错了导致的~
# categrouy_id 我要留个纪念

class SearchViewSKU(HaystackViewSet):
    """
    商品的搜索视图类
    """
    # 指定分页器
    pagination_class = SKUListPageNumberPagination
    index_models = [SKU]
    serializer_class = SKUIndexHaystackSerializer